package com.cdmu.controller;

import com.cdmu.constant.ResultConstant;
import com.cdmu.constant.ResultObj;
import com.cdmu.model.ActiveUser;
import com.cdmu.pojo.User;
import com.cdmu.service.IUserService;
import com.cdmu.utils.SessionUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

/**
 * @author BenOniShi
 * @date 2020/4/21 22:43
 */
@RequestMapping("/login")

@Controller
public class LoginController {
  
	@Autowired
    @Lazy  //只有使用的时候才会加载
    private IUserService userService;
    /**
     * 登录方法
     */
    @RequestMapping(value = "/doLogin", method = RequestMethod.POST)
    @ResponseBody
    public ResultObj doLogin(String userName, String userPwd) {
		/*
		 * 去掉shiro验证
		 * */
    	/*
		 * Subject subject = SecurityUtils.getSubject(); AuthenticationToken token = new
		 * UsernamePasswordToken(userName, userPwd);
		 */
        try {
			/*
			 * subject.login(token); ActiveUser user = (ActiveUser) subject.getPrincipal();
			 * HttpSession session = SessionUtil.getSession();
			 * session.setMaxInactiveInterval(60 * 60 * 12); 
			 * session.setAttribute("user",
			 * user.getUser());
			 */
        	User user = userService.queryEmpByName(userName);
        	ActiveUser aUser = new ActiveUser();
            aUser.setUser(user);
            HttpSession session = SessionUtil.getSession();
		     session.setMaxInactiveInterval(60 * 60 * 12); 
			 session.setAttribute("user",aUser.getUser());
        	//TODO 登录逻辑完善
        	return new ResultObj(ResultConstant.SUCCESS_CODE,"登录成功");
        } catch (AuthenticationException e) {
            e.printStackTrace();
            return new ResultObj(ResultConstant.FAILED_CODE,"登录失败");
        }
    }


    /**
     * 退出 删除session 返回成功信息
     *
     * @return
     */
    @RequestMapping(value = "/logout", method = RequestMethod.GET)
    public String logOut() {
        Subject subject = SecurityUtils.getSubject();
        subject.logout();
        return "login";
    }


}
